﻿$(document).ready(function () {
    function loadchart(early,late,absent_Y,absent_N) {
        var randomScalingFactor = function () {
            return Math.round(Math.random() * 100);
        };
        var randomColorFactor = function () {
            return Math.round(Math.random() * 255);
        };
        var randomColor = function (opacity) {
            return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
        };

        var config = {
            type: 'doughnut',
            data: {
                datasets: [{
                    data: [
                        early,
                        late,
                        absent_Y,
                        absent_N
                        //randomScalingFactor()
                    ],
                    backgroundColor: [
                        randomColor(1),
                        randomColor(1),
                        randomColor(1),
                        randomColor(1)
                    ],
                    label: 'TỶ LỆ ĐI HỌC CỦA HỌC VIÊN'
                }],
                labels: [
                    "Đi học đúng giờ",
                    "Đi học trễ / về sớm",
                    "Vắng có phép",
                    "Vắng không phép"
                ]
            },
            options: {
                responsive: true,
                legend: {
                    position: 'bottom',
                },
                title: {
                    display: true,
                    text: 'TỶ LỆ ĐI HỌC CỦA HỌC VIÊN'
                },
                animation: {
                    animateScale: true,
                    animateRotate: true
                }
            }
        };

        var ctx = document.getElementById("chart-area").getContext("2d");
        window.myDoughnut = new Chart(ctx, config);
    }
    var early = document.getElementById("early").innerHTML;
    var late = document.getElementById("late").innerHTML;
    var absent_yes = document.getElementById("absent_yes").innerHTML;
    var absent_no = document.getElementById("absent_no").innerHTML;
    window.onload = loadchart(early,late,absent_yes,absent_no);

    $("#report_student_type").on("change", function () {
        var type = document.getElementById("report_student_type").value;
        if (type == 1) {
            document.getElementById("report_student_class").style.display = 'none';
            document.getElementById("report_student_teacher").style.display = 'none';
        }
        if (type == 2) {
            document.getElementById("report_student_class").style.display = 'inline-block';
            document.getElementById("report_student_teacher").style.display = 'none';

        }
        if (type == 3) {
            document.getElementById("report_student_class").style.display = 'none';
            document.getElementById("report_student_teacher").style.display = 'inline-block';
        }
    });
    $("#report_student_time").on("change", function () {
        var type = document.getElementById("report_student_time").value;
        if (type == 0) {
            document.getElementById("report_student_month").style.display = "none";
            document.getElementById("report_student_year").style.display = 'none';
            document.getElementById("report_student_day").style.display = "none";
        }
        if (type == 1) {
            document.getElementById("report_student_month").style.display = "none";
            document.getElementById("report_student_year").style.display = 'none';
            document.getElementById("report_student_day").style.display = "inline-block";
        }
        if (type == 2) {
            document.getElementById("report_student_month").style.display = "inline-block";
            document.getElementById("report_student_year").style.display = 'inline-block';
            document.getElementById("report_student_day").style.display = "none";
        }
        if (type == 3) {
            document.getElementById("report_student_month").style.display = "none";
            document.getElementById("report_student_year").style.display = 'inline-block';
            document.getElementById("report_student_day").style.display = "none";
        }
    });

    $(".btn_report_student").on("click", function () {
        
        var type = document.getElementById("report_student_type").value;

        //if (type == 1) {
        //    var time = document.getElementById("report_student_time").value;
        //    if (time == 1) {
        //        var date = document.getElementById("report_student_day").value;
        //        var xhttp;
        //        xhttp = new XMLHttpRequest();
        //        xhttp.onreadystatechange = function () {
        //            if (xhttp.readyState == 4 || xhttp.status == 200) {
        //                document.getElementById('number_area').innerHTML = xhttp.responseText;
        //                var early = document.getElementById("early").innerHTML;
        //                var late = document.getElementById("late").innerHTML;
        //                var absent_yes = document.getElementById("absent_yes").innerHTML;
        //                var absent_no = document.getElementById("absent_no").innerHTML;
        //                document.getElementById("myCanvas").innerHTML = "";
        //                document.getElementById("myCanvas").innerHTML = "<canvas id='chart-area' />";

        //                loadchart(early, late, absent_yes, absent_no);
        //            }
        //        };
        //        xhttp.open("GET", "../ajax/asp/reports/report_student_day.aspx?date=" + date, true);
        //        xhttp.send();
        //    }
        //    if (time == 2) {
        //        var month = document.getElementById("report_student_month").value;
        //        var year = document.getElementById("report_student_year").value;
        //        var xhttp;
        //        xhttp = new XMLHttpRequest();
        //        xhttp.onreadystatechange = function () {
        //            if (xhttp.readyState == 4 || xhttp.status == 200) {
        //                document.getElementById('number_area').innerHTML = xhttp.responseText;
        //                var early = document.getElementById("early").innerHTML;
        //                var late = document.getElementById("late").innerHTML;
        //                var absent_yes = document.getElementById("absent_yes").innerHTML;
        //                var absent_no = document.getElementById("absent_no").innerHTML;
        //                document.getElementById("myCanvas").innerHTML = "";
        //                document.getElementById("myCanvas").innerHTML = "<canvas id='chart-area' />";

        //                loadchart(early, late, absent_yes, absent_no);
        //            }
        //        };
        //        xhttp.open("GET", "../ajax/asp/reports/report_student_month.aspx?month=" + month +"&year="+year, true);
        //        xhttp.send();
        //    }
        //    if (time == 3) {
        //        var year = document.getElementById("report_student_year").value;
        //        var xhttp;
        //        xhttp = new XMLHttpRequest();
        //        xhttp.onreadystatechange = function () {
        //            if (xhttp.readyState == 4 || xhttp.status == 200) {
        //                document.getElementById('number_area').innerHTML = xhttp.responseText;
        //                var early = document.getElementById("early").innerHTML;
        //                var late = document.getElementById("late").innerHTML;
        //                var absent_yes = document.getElementById("absent_yes").innerHTML;
        //                var absent_no = document.getElementById("absent_no").innerHTML;
        //                document.getElementById("myCanvas").innerHTML = "";
        //                document.getElementById("myCanvas").innerHTML = "<canvas id='chart-area' />";

        //                loadchart(early, late, absent_yes, absent_no);
        //            }
        //        };
        //        xhttp.open("GET", "../ajax/asp/reports/report_student_year.aspx?year=" + year, true);
        //        xhttp.send();
        //    }
        //}

        if (type == 2) {
            var id = document.getElementById("report_student_class").value;
            var time = document.getElementById("report_student_time").value;
            if (time == 1) {
                var day = document.getElementById("report_student_day").value;
                
                var xhttp;
                xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function () {
                    if (xhttp.readyState == 4 || xhttp.status == 200) {
                        document.getElementById('number_area').innerHTML = xhttp.responseText;
                        var early = document.getElementById("early").innerHTML;
                        var late = document.getElementById("late").innerHTML;
                        var absent_yes = document.getElementById("absent_yes").innerHTML;
                        var absent_no = document.getElementById("absent_no").innerHTML;
                        document.getElementById("myCanvas").innerHTML = "";
                        document.getElementById("myCanvas").innerHTML = "<canvas id='chart-area' />";

                        loadchart(early, late, absent_yes, absent_no);
                    }
                };
                xhttp.open("GET", "../ajax/asp/reports/report_student_class.aspx?id=" + id +"&date="+day +"&type="+time, true);
                xhttp.send();
      
            }
            if (time == 2) {
                var month = document.getElementById("report_student_month").value;
                var year = document.getElementById("report_student_year").value;
                var date = year + "-" + month + "-" + 1;
                var xhttp;
                xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function () {
                    if (xhttp.readyState == 4 || xhttp.status == 200) {
                        document.getElementById('number_area').innerHTML = xhttp.responseText;
                        var early = document.getElementById("early").innerHTML;
                        var late = document.getElementById("late").innerHTML;
                        var absent_yes = document.getElementById("absent_yes").innerHTML;
                        var absent_no = document.getElementById("absent_no").innerHTML;
                        document.getElementById("myCanvas").innerHTML = "";
                        document.getElementById("myCanvas").innerHTML = "<canvas id='chart-area' />";

                        loadchart(early, late, absent_yes, absent_no);
                    }
                };
                xhttp.open("GET", "../ajax/asp/reports/report_student_class.aspx?id=" + id + "&date=" + date + "&type=" + time, true);
                xhttp.send();
            }
            if (time == 3) {
                var year = document.getElementById("report_student_year").value;
                var date = year + "-1-1";
                var xhttp;
                xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function () {
                    if (xhttp.readyState == 4 || xhttp.status == 200) {
                        document.getElementById('number_area').innerHTML = xhttp.responseText;
                        var early = document.getElementById("early").innerHTML;
                        var late = document.getElementById("late").innerHTML;
                        var absent_yes = document.getElementById("absent_yes").innerHTML;
                        var absent_no = document.getElementById("absent_no").innerHTML;
                        document.getElementById("myCanvas").innerHTML = "";
                        document.getElementById("myCanvas").innerHTML = "<canvas id='chart-area' />";

                        loadchart(early, late, absent_yes, absent_no);
                    }
                };
                xhttp.open("GET", "../ajax/asp/reports/report_student_class.aspx?id=" + id + "&date=" + date + "&type=" + time, true);
                xhttp.send();
            }
        }

        if (type == 3) {
            var id = document.getElementById("report_student_teacher").value;
            var time = document.getElementById("report_student_time").value;
            if (time == 1) {
                var day = document.getElementById("report_student_day").value;
                var xhttp;
                xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function () {
                    if (xhttp.readyState == 4 || xhttp.status == 200) {
                        document.getElementById('number_area').innerHTML = xhttp.responseText;
                        var early = document.getElementById("early").innerHTML;
                        var late = document.getElementById("late").innerHTML;
                        var absent_yes = document.getElementById("absent_yes").innerHTML;
                        var absent_no = document.getElementById("absent_no").innerHTML;
                        document.getElementById("myCanvas").innerHTML = "";
                        document.getElementById("myCanvas").innerHTML = "<canvas id='chart-area' />";

                        loadchart(early, late, absent_yes, absent_no);
                    }
                };
                xhttp.open("GET", "../ajax/asp/reports/report_student_teacher.aspx?id=" + id + "&date=" + day + "&type=" + time, true);
                xhttp.send();
            }
            if (time == 2) {
                var month = document.getElementById("report_student_month").value;
                var year = document.getElementById("report_student_year").value;
                var date = year + "-" + month + "-" + 1;
                var xhttp;
                xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function () {
                    if (xhttp.readyState == 4 || xhttp.status == 200) {
                        document.getElementById('number_area').innerHTML = xhttp.responseText;
                        var early = document.getElementById("early").innerHTML;
                        var late = document.getElementById("late").innerHTML;
                        var absent_yes = document.getElementById("absent_yes").innerHTML;
                        var absent_no = document.getElementById("absent_no").innerHTML;
                        document.getElementById("myCanvas").innerHTML = "";
                        document.getElementById("myCanvas").innerHTML = "<canvas id='chart-area' />";

                        loadchart(early, late, absent_yes, absent_no);
                    }
                };
                xhttp.open("GET", "../ajax/asp/reports/report_student_teacher.aspx?id=" + id + "&date=" + date + "&type=" + time, true);
                xhttp.send();
            }
            if (time == 3) {
                var year = document.getElementById("report_student_year").value;
                var date = year + "-1-1";
                var xhttp;
                xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function () {
                    if (xhttp.readyState == 4 || xhttp.status == 200) {
                        document.getElementById('number_area').innerHTML = xhttp.responseText;
                        var early = document.getElementById("early").innerHTML;
                        var late = document.getElementById("late").innerHTML;
                        var absent_yes = document.getElementById("absent_yes").innerHTML;
                        var absent_no = document.getElementById("absent_no").innerHTML;
                        document.getElementById("myCanvas").innerHTML = "";
                        document.getElementById("myCanvas").innerHTML = "<canvas id='chart-area' />";

                        loadchart(early, late, absent_yes, absent_no);
                    }
                };
                xhttp.open("GET", "../ajax/asp/reports/report_student_teacher.aspx?id=" + id + "&date=" + date + "&type=" + time, true);
                xhttp.send();
            }
            
            
        }

    });
});